package com.ayf.payment.game.api.aspect;

import com.swwx.charm.commons.lang.utils.LogPortal;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LogAspect {

    /**
     * @param joinPoint
     * @return
     * @throws Throwable
     */
    @Around("execution(public * com.ayf.payment.game.api.controller.*.*(..)) || execution(public * com.ayf.payment.game.api.service..*.*(..))")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature method = (MethodSignature) joinPoint.getSignature();
        Object[] args = joinPoint.getArgs();
        StringBuilder sb = new StringBuilder();
        for (Object object : args) {
            sb.append(object == null ? null : object.toString()).append(" | ");
        }
        long start = System.currentTimeMillis();
        Object obj = null;
        try {
            obj = joinPoint.proceed();
        } finally {
            long end = System.currentTimeMillis();
            LogPortal.info("执行时间:{}ms,执行类名:{},执行方法:{},传入参数:{},返回数据:{}", (end - start),
                    joinPoint.getTarget().getClass().getName(), method.getName(), sb.toString(), (obj == null ? null : obj.toString()));
        }
        return obj;
    }

}
